package com.ridu.project.beshid.controller;

import com.ridu.project.beshid.api.GeneralResponse;
import com.ridu.project.beshid.api.Status;
import com.ridu.project.beshid.api.User;
import com.ridu.project.beshid.entity.UserEntity;
import com.ridu.project.beshid.service.JifenReqService;
import com.ridu.project.beshid.service.JifenService;
import com.ridu.project.beshid.utils.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by user on 2018/5/22.
 */
@Controller
@RequestMapping(value = "jifen",produces = "application/json")
@Slf4j
public class JifenController extends BaseController {

    @Autowired
    private JifenReqService jifenReqService;
    @Autowired
    private JifenService service;

    @RequestMapping(value = "redirectSys", method= RequestMethod.POST)
    public String redirectSys(String username) {
        request.setAttribute("username", username);
        return "jifen_admin";
    }

    @RequestMapping(value = "toJifenAudit", method= RequestMethod.POST)
    public String toJifenAudit() {
        return "jifen_audit";
    }

    @RequestMapping(value = "toJifenBuy")
    public String toJifenBuy() {
        return "jifen_buy";
    }

    /**
     * 获取积分
     * @return
     */
    @RequestMapping(value = "getJifen", method= RequestMethod.GET)
    @ResponseBody
    public GeneralResponse getJifen(@RequestParam int currentPage, @RequestParam int pageSize, String username,String flow,String source,String type,
                                    String startTime, String endTime){
        GeneralResponse response = new GeneralResponse();
        try {
            if(StringUtil.isEmpty(username)){
                return jifenReqService.findAll(currentPage, pageSize, getUser().getUsername(), flow, source, type, startTime, endTime);
            } else {
                return jifenReqService.findAll(currentPage, pageSize, username, flow, source, type, startTime, endTime);
            }
        } catch (Exception e) {
            response.setStatus(Status.FAILED);
            response.setMessage(e.getMessage());
        }
        return response;
    }

    /**
     * 获取积分
     * @return
     */
    @RequestMapping(value = "getJifenAuditList", method= RequestMethod.GET)
    @ResponseBody
    public GeneralResponse getJifenAuditList(@RequestParam int currentPage, @RequestParam int pageSize){
        GeneralResponse response = new GeneralResponse();
        try {
            return service.getJifensAuditList(currentPage, pageSize, null);
        } catch (Exception e) {
            response.setStatus(Status.FAILED);
            response.setMessage(e.getMessage());
        }
        return response;
    }

    /**
     * 获取积分
     * @return
     */
    @RequestMapping(value = "getStream", method= RequestMethod.GET)
    @ResponseBody
    public GeneralResponse getStream(@RequestParam int currentPage, @RequestParam int pageSize,String startTime, String endTime, String category){
        GeneralResponse response = new GeneralResponse();
        try {
            return service.getStream(currentPage, pageSize, startTime, endTime, category);
        } catch (Exception e) {
            response.setStatus(Status.FAILED);
            response.setMessage(e.getMessage());
        }
        return response;
    }


    @RequestMapping(value = "getExchangeRate")
    @ResponseBody
    public GeneralResponse getExchangeRate() {
        GeneralResponse response = new GeneralResponse();
        try{
            UserEntity user = getUser();
            if(user == null){
                throw new Exception("请重新登录!");
            }
            User u = new User();
            BeanUtils.copyProperties(user, u);
            response.setData(u);
            response.setStatus(Status.SUCCESS);
        } catch (Exception e){
            response.setStatus(Status.FAILED);
            response.setMessage(e.getMessage());
        }
        return response;
    }

    @RequestMapping(value = "setExchangeRate", method= RequestMethod.POST)
    @ResponseBody
    public GeneralResponse setExchangeRate(String rate) {
        GeneralResponse response = new GeneralResponse();
        try{
            UserEntity user = getUser();
            if(user == null){
                throw new Exception("请重新登录!");
            }
            user.setRate(rate);
            return jifenReqService.setExchangeRate(user);
        } catch (Exception e){
            response.setStatus(Status.FAILED);
            response.setMessage(e.getMessage());
        }
        return response;
    }

    @RequestMapping(value = "jifenBuy", method= RequestMethod.POST)
    @ResponseBody
    public GeneralResponse jifenBuy(@RequestParam String category, @RequestParam String jifen_num) {
        GeneralResponse response = new GeneralResponse();
        try{
            UserEntity user = getUser();
            if(user == null){
                throw new Exception("请重新登录!");
            }
            return jifenReqService.jifenBuy(user, category, jifen_num);
        } catch (Exception e){
            response.setStatus(Status.FAILED);
            response.setMessage(e.getMessage());
        }
        return response;
    }

    @RequestMapping(value = "sent", method= RequestMethod.POST)
    @ResponseBody
    public GeneralResponse jifenSent(@RequestParam String mobiles){
        GeneralResponse response = new GeneralResponse();
        try {
            return service.sent(mobiles);
        } catch (Exception e) {
            response.setStatus(Status.FAILED);
            response.setMessage(e.getMessage());
        }
        return response;
    }

    @RequestMapping(value = "getDetail")
    @ResponseBody
    public GeneralResponse getDetail(@RequestParam String transactionHash){
        return service.getDetail(transactionHash);
    }

}
